<?php

namespace App\Providers;

use App\Http\ViewComposers\ProfileComposer;
use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;
use View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

        $this->mapDingoAPISetting();

        $this->mapViewShare();
        $this->mapCommonConfig();
    }
    protected function mapViewShare()
    {
        $age = Carbon::createFromDate(1993, 2, 2)->age;

        View::share('name', 'value');
        View::share('age', $age);
//        View::composer('admin.posts.index', function ($view) {
//
//        });
        View::composer('*', function ($view) {
            /**
             * @var $view \Illuminate\View\View
             */
            return $view->with('auth', \Auth::user());
        });
//        View::composer('*','App\Http\ViewComposers\ProfileComposer');
        View::composer('*', ProfileComposer::class);
    }

    protected function mapCommonConfig()
    {
//        // Make a custom blade directive:
//        Blade::directive('shout', function ($string) {
//            return trim(strtoupper($string), '(\'\')');
//        });
//
//        // And another one for good measure:
//        Blade::directive('customLink', function () {
//            return '<a href="#">Custom Link</a>';
//        });
        //
        \Carbon\Carbon::setLocale('zh');
        /**
         * storage/logs 文件夹下面的 laravel log
         */
//        \DB::listen(function($query) {
//            /**
//             *@var $query  QueryExecuted
//             */
////            \Log::info($query->sql.serialize($query->bindings).$query->connectionName.$query->time);
//            \Log::info($query->sql.json_encode($query->bindings).$query->connectionName.$query->time);
//        });

//        \DB::listen(function($sql, $bindings, $time) {
//            echo 'SQL语句执行：'.$sql.'，参数：'.json_encode($bindings).',耗时：'.$time.'ms';
//        });
    }

    /**
     * Register any application services.
     * @return void
     */
    public function register()
    {
        $this->app->register(SomeServiceProvider::class);
        if ($this->app->environment() !== 'production') {
            $this->app->register(\Laracasts\Generators\GeneratorsServiceProvider::class);
            $this->app->register(\Way\Generators\GeneratorsServiceProvider::class);
            $this->app->register(\Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider::class);
        }
    }

    private function mapDingoAPISetting()
    {
//       //Authentication Providers
//        $this->app['Dingo\Api\Auth\Auth']->extend('oauth', function ($app) {
//            return new \Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
//        });
//        //Throttling / Rate Limiting
//        $this->app['Dingo\Api\Http\RateLimit\Handler']->extend(function ($app) {
//            return new \Dingo\Api\Http\RateLimit\Throttle\Authenticated;
//        });
//        //Response Transformer
//        $this->app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
//            $fractal = new \League\Fractal\Manager;
//            $fractal->setSerializer(new \League\Fractal\Serializer\JsonApiSerializer);
//            return new \Dingo\Api\Transformer\Adapter\Fractal($fractal);
//        });
//        //Response Formats
//        \Dingo\Api\Http\Response::addFormatter('json', new \Dingo\Api\Http\Response\Format\Jsonp);
//        //Error Format
//        $this->app['Dingo\Api\Exception\Handler']->setErrorFormat([
//            'error' => [
//                'message' => ':message',
//                'errors' => ':errors',
//                'code' => ':code',
//                'status_code' => ':status_code',
//                'debug' => ':debug'
//            ]
//        ]);

    }


}
